package edu.mercer.sse.plainpdf;

import java.util.Map;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import edu.mercer.sse.annotations.Fulfills;
import edu.mercer.sse.configuration.Config;

/**
 * Read Configuration, extracting relevant keys and their values.<p>
 * 
 * Matches utilityKey String against Configuration Map collection keys. Used 
 * 	for retrieval of specific utility's configuration.<p>
 * 
 * Example <code>config.xml</code> excerpt:<p>
 * 
 * <code>&#60;entry key="PDFINFO_ID"&#62; PDFINFO &#60;&#47;entry&#62;
 * </code>
 * <p>
 * 
 * Example utilityKey value:<p>
 * 
 * <ul><li>"PDFINFO"</ul>
 * 
 * @see Configuration
 * 
 * @see Function
 */

public class ParseConfigurationFunction implements 

	Function<String, Map<String,String>> 
{
	@Fulfills("Function")
	@Override
	public ImmutableMap<String, String> apply( final String utilityKey ) 
	{
		final Map<String,String> configMap = Config.factory()
												   .load()
												   .getItems();

		/* 
		 * Trim each value in map which has a key matching the regular 
		 * expression  
		 */
		
		return ImmutableMap.copyOf( Maps.filterKeys( configMap, 
						
				Predicates.containsPattern("^" + utilityKey + "_")));
		
	}//method

	
}//class
